package com.naiterui.ehp.bs.doctor.repository;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.naiterui.common.repository.core.BaseJpaRepository;
import com.naiterui.ehp.bp.domain.ConsultSession;

@Repository
public interface IConsultSessionRepository extends BaseJpaRepository<ConsultSession, Long> {


    List<ConsultSession> findBySessionId(String sessionId);

    List<ConsultSession> findByDoctorIdAndPatientIdInAndStartTimeGreaterThanEqual(Long doctorId, List<Long> patientIds, Date date);

    List<ConsultSession> findByDoctorIdAndStartTimeGreaterThanAndStartTimeLessThanEqual(Long doctorId, Date timeGt, Date timeLt);

    @Query("select c.inquirerId from ConsultSession c where c.patientId=?1 and c.doctorId=?2 group by c.inquirerId")
    List<Long> findByPatientIdAndDoctorId(Long patientId, Long doctorId);

    List<ConsultSession> findByEndTimeBetweenAndEndTimeIsNotNull(Date start, Date end);

    ConsultSession findFirstBySessionId(String sessionId);

    List<ConsultSession> findBySessionIdIn(Collection<String> sessionIds);

    /**
     * 获取最新的一条会话
     *
     * @param doctorId   医生id
     * @param patientId  患者id
     * @param inquirerId 就诊人id
     * @return
     */
    ConsultSession findFirstByDoctorIdAndPatientIdAndInquirerIdAndEndTimeIsNullOrderByStartTimeDesc(Long doctorId, Long patientId, Long inquirerId);

    @Query(value = "select COUNT(id) from ConsultSession where doctorId = ?1 AND endTime IS NOT NULL")
    Integer findCountByDoctorId(Long doctorId);

    /**
     * 查询该医生所有未结束的会话
     * @param doctorId
     * @return
     */
    List<ConsultSession> findByDoctorIdAndEndTimeIsNull(Long doctorId);

    List<ConsultSession> findAllByIdIn(Collection<Long> ids);
}
